﻿using System;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Roles
{
    public partial class manageAllRoles : SecurityPage
    {
        public void AddRole(object sender, EventArgs e)
        {
            if (!textBox1.Text.Equals(string.Empty))
            {
                try
                {
                    CallWebAdminHelperMethod(false, "CreateRole", new object[] {textBox1.Text}, new[] {typeof (string)});
                    textBox1.Text = string.Empty;
                }
                catch (Exception ex)
                {
                    errorMessage.Text = ex.Message;
                    errorMessage.Visible = true;
                }
                BindGrid();
            }
        }

        private void BindGrid()
        {
            var arr = (string[]) CallWebAdminHelperMethod(false, "GetAllRoles", new object[] {}, null);
            dataGrid.DataSource = arr;

            int currentPage = dataGrid.PageIndex;
            int count = arr.Length;
            int pageSize = dataGrid.PageSize;
            if (count > 0 && currentPage == count/pageSize && count%pageSize == 0)
            {
                dataGrid.PageIndex -= 1;
            }
            dataGrid.DataBind();
            dataGrid.Visible = (dataGrid.Rows.Count > 0);
        }

        public string GetToolTip(string resourceName, string itemName)
        {
            var tempString = (string) GetLocalResourceObject(resourceName);
            return String.Format((string) GetGlobalResourceObject("GlobalResources", "ToolTipFormat"), tempString,
                                 itemName);
        }

        public void IndexChanged(object sender, GridViewPageEventArgs e)
        {
            dataGrid.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        public void LinkButtonClick(object sender, CommandEventArgs e)
        {
            if (e.CommandName.Equals("ManageRole"))
            {
                CurrentRole = (string) e.CommandArgument;
                // do not prepend ~/ to this path since it is not at the root
                Response.Redirect("manageSingleRole.aspx");
            }
            if (e.CommandName.Equals("DeleteRole"))
            {
                RoleName.Text = (string) e.CommandArgument;
                ((WebAdminWithConfirmation) Master).SetDisplayUI(true);
            }
        }


        public void No_Click(object sender, EventArgs e)
        {
            ((WebAdminWithConfirmation) Master).SetDisplayUI(false);
        }


        public void Page_Load()
        {
            if (!IsPostBack)
            {
                BindGrid();
            }

            dataGrid.HeaderStyle.HorizontalAlign = DirectionalityHorizontalAlign;
        }

        public void Yes_Click(object sender, EventArgs e)
        {
            if (
                (bool)
                CallWebAdminHelperMethod(false, "RoleExists", new object[] {RoleName.Text}, new[] {typeof (string)}))
            {
                CallWebAdminHelperMethod(false, "DeleteRole", new object[] {RoleName.Text, false},
                                         new[] {typeof (string), typeof (bool)});
            }

            BindGrid();
            ((WebAdminWithConfirmation) Master).SetDisplayUI(false);
        }
    }
}